/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.view.gui.listar;

import app.entities.model.DTO.MatriculaAlunoDTO;
import app.entities.model.DTO.TurmaDTO;
import app.view.classes.ViewTurma;
import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Murilo
 */
public class ListaTurma extends javax.swing.JFrame {
    
    private ArrayList<TurmaDTO> listaTurmasProfessor;
    private ViewTurma viewTurma;
    private TurmaDTO turma;
    private String codigoProfessor;
    private JTextField nomeMateria;
    private JTable tblAlunos;
    
    public ListaTurma(JTable tabelaAlunos,JTextField edtMateria, TurmaDTO turma, String codigoMatriculaProfessor) {
        initComponents();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        pack();
        setLocation((screenSize.width/2) - (getWidth()/2), (screenSize.height/2) - (getHeight()/2));
        this.viewTurma = new ViewTurma();
        this.turma = turma;
        this.codigoProfessor = codigoMatriculaProfessor;
        this.nomeMateria = edtMateria;
        this.tblAlunos = tabelaAlunos;
        this.listaTurmasProfessor = this.viewTurma.retornarListaTurmas(codigoProfessor);
        DefaultTableModel model = (DefaultTableModel) this.tblTurmas.getModel();
        for(TurmaDTO turmaAux : this.listaTurmasProfessor){
            model.addRow(new Object[]{turmaAux.getId(), turmaAux.getNomeMateria()});
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblTurmas = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Turmas");

        tblTurmas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Nome Matéria"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblTurmas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblTurmasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblTurmas);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblTurmasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblTurmasMouseClicked
        if(tblTurmas.getSelectedRow() != -1){
            int RegistroSelecionado = this.tblTurmas.getSelectedRow();
            String Id = String.valueOf(((DefaultTableModel) this.tblTurmas.getModel()).getValueAt(RegistroSelecionado, 0));
            for(TurmaDTO turmaAux : this.listaTurmasProfessor){
                if(Integer.valueOf(Id) == turmaAux.getId()){
                    this.turma.setId(turmaAux.getId());
                    this.turma.setNomeMateria(turmaAux.getNomeMateria());
                    this.turma.setProfessor(turmaAux.getProfessor());
                    this.turma.setAlunos(turmaAux.getAlunos());
                    this.turma.setBimestres(turmaAux.getBimestres());
                }
            }
            DefaultTableModel model = (DefaultTableModel) this.tblAlunos.getModel();
            for(MatriculaAlunoDTO matAluno : this.turma.getAlunos()){
                model.addRow(new Object[]{matAluno.getCodigoMatricula(), matAluno.getAluno().getNome(),
                    matAluno.getAluno().getSobrenome(), 0});
            }
            this.nomeMateria.setText(this.turma.getNomeMateria());
            this.dispose();
        }
    }//GEN-LAST:event_tblTurmasMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblTurmas;
    // End of variables declaration//GEN-END:variables
}
